<?php
/**
 * User Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\user;

use app\admin\model\user\User as UserModels;

Class User {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'mobile') ) {
            $map[] = ['mobile', '=', $get['mobile']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return UserModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 新增优惠券信息
     * @param array $data 数据包
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $data['type'] = implode(',', $data['type']);
        $coupons = CouponsModels::create($data, ['start_date','end_date','type','price']);
        return $coupons->isEmpty()?false:$coupons->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @return boolean
     */
    public function destory( $data )
    {
        return CouponsModels::destroy(function($query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

    /**
     * 优惠券管理编辑
     * @param UserModels $model
     * @param array $data
     */
    public function save(UserModels $model, array $data)
    {
        $model->password = $data['password'];
        return $model->save();
    }

}